﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using System.IO.Ports;
using System.Text.RegularExpressions;
using System.Threading;
using System.Reflection; // 引用这个才能使用Missing字段
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Xml;
using System.Web;
using System.Runtime.InteropServices;
using ZedGraph;

namespace weiqijiance
{
    public partial class TESTDLX : Form
    {

        public string sshebei = "动力性";
        public string sdetid;
        public mainform m_mainform;
        /// <summary>
        /// 结果变量
        /// </summary>
        public float fsudu;
        public float fjiazaili;

        public Thread t1;
        public Thread t2;
        public string xianshineirong;
        public string businessId;
        public int xssj;

        public int iDLXHWQ;//动力性到位红外

        public int iDLXKZJSS;//动力性举升升
        public int iDLXKZJSJ;//动力性举升降
        public int iFENGSHAN=-1;////测功机风扇

        public int iDLXtdz;//动力性信号左通道
        public int iDLXtdy;//动力性信号右通道
        public int iDLXtdh;//动力性信号右通道

        public int iDLXSDtd;//车速信号接在那个通道


        //红外取反
        public bool[] bhongwaiqf = new bool[16] { false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false };//红外是否取反
        //红外定位时间
        public int[] ihongwaidwsj = new int[16] { 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000 };//红外定位时间单位毫秒




        // /声明INI文件的写操作函数 WritePrivateProfileString()
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        //声明INI文件的读操作函数 GetPrivateProfileString()
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        private string sPath = null;

        public 控制流程.flowcrtl flowbass = new 控制流程.flowcrtl();

        public TESTDLX(int Width, int Height)
        {
            InitializeComponent();
             panel_Width = Width;
            panel_Height=Height;
            //this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            x = this.Width;
            y = this.Height;
            setTag(this);
            xinxixianshi.Visible = true;
            xinxixianshi.Text = "动力性检测";
            gettongdao(); 
            init();
            bianliangchushihuaJC();
        }
        public void start()//(string sbusinessId)
        {
            //businessId = sbusinessId;
            bianliangchushihua();
            t1 = new Thread(new ThreadStart(Thread1));
            //t2 = new Thread(new ThreadStart(Thread2));
            //t2.Priority = ThreadPriority.Lowest;
            t1.Start();

        }
        /// <summary>
        /// 变量初始化
        /// </summary>
        public void bianliangchushihua()
        {
            fsudu = 0;
            fjiazaili = 0;

            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\车辆信息.ini";
            string path1 = string.Format(@"{0}", path);
            Ini(path1);

        }
        #region 控件大小随窗体大小等比例缩放
        private float x;//定义当前窗体的宽度
        private float y;//定义当前窗体的高度
        private void setTag(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                con.Tag = con.Width + ";" + con.Height + ";" + con.Left + ";" + con.Top + ";" + con.Font.Size;
                if (con.Controls.Count > 0)
                {
                    setTag(con);
                }
            }
        }
        private void setControls(float newx, float newy, Control cons)
        {
            if (newy <= 0)
            {
                newy = 1;
            }
            //遍历窗体中的控件，重新设置控件的值
            foreach (Control con in cons.Controls)
            {
                //获取控件的Tag属性值，并分割后存储字符串数组
                if (con.Tag != null)
                {
                    string[] mytag = con.Tag.ToString().Split(new char[] { ';' });
                    //根据窗体缩放的比例确定控件的值
                    con.Width = Convert.ToInt32(System.Convert.ToSingle(mytag[0]) * newx);//宽度
                    con.Height = Convert.ToInt32(System.Convert.ToSingle(mytag[1]) * newy);//高度
                    con.Left = Convert.ToInt32(System.Convert.ToSingle(mytag[2]) * newx);//左边距
                    con.Top = Convert.ToInt32(System.Convert.ToSingle(mytag[3]) * newy);//顶边距
                    Single currentSize = System.Convert.ToSingle(mytag[4]) * newy;//字体大小
                    con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                    if (con.Controls.Count > 0)
                    {
                        setControls(newx, newy, con);
                    }
                }
            }
        }
        #endregion

        //ini文件
        public void Ini(string path)
        {
            this.sPath = path;
        }
        public void Writue(string section, string key, string value)
        {
            // section=配置节，key=键名，value=键值，path=路径
            WritePrivateProfileString(section, key, value, sPath);
        }
        public string ReadValue(string section, string key)
        {
            // 每次从ini中读取多少字节
            System.Text.StringBuilder temp = new System.Text.StringBuilder(255);

            // section=配置节，key=键名，temp=上面，path=路径
            GetPrivateProfileString(section, key, "", temp, 255, sPath);
            return temp.ToString();
        }
        //ini文件
        public void gettongdao()
        {

            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\串口及通道设置.ini";
            string path1 = string.Format(@"{0}", path);
            Ini(path1);
            for (int i = 0; i < 10; i++)
            {
                string sgetname = ReadValue("通道设置", string.Format(@"模拟信号{0}", i + 1));
                if (sgetname.Contains("测功力左"))
                {
                    iDLXtdz = i;
                }

                if (sgetname.Contains("测功力右"))
                {
                    iDLXtdy = i;
                }

                if (sgetname.Contains("测功力后"))
                {
                    iDLXtdh = i;
                }
            }

            for (int i = 0; i < 4; i++)
            {
                string smaichong = ReadValue("通道设置", string.Format(@"速度{0}", i + 1));
                if (smaichong.Contains("测功速度"))
                {
                    iDLXSDtd = i;
                }

            }

            for (int i = 0; i < 16; i++)
            {
                string shongwai = ReadValue("通道设置", string.Format(@"红外{0}", i + 1));
                if (shongwai.Contains("车速到位"))
                {
                    iDLXHWQ = i;
                }


                shongwai = ReadValue("通道设置", string.Format(@"红外定位时间{0}", i + 1));
                int.TryParse(shongwai, out ihongwaidwsj[i]);


                shongwai = ReadValue("通道设置", string.Format(@"红外是否取反{0}", i + 1));
                if (shongwai.Contains("是"))
                {
                    bhongwaiqf[i] = true;
                }
                else
                {
                    bhongwaiqf[i] = false;
                }
            }
            ///控制
            for (int i = 0; i < 8; i++)
            {

                string shongwai = ReadValue("通道设置", string.Format(@"控制{0}", i + 1));
                if (shongwai.Contains("测功举升升"))
                {
                    iDLXKZJSS = i;
                }
                if (shongwai.Contains("测功举升降"))
                {
                    iDLXKZJSJ = i;
                }
                if (shongwai.Contains("测功机风扇"))
                {
                    iFENGSHAN = i;
                }
            }
        }

        private delegate void FlushClient();//代理

        private void xianshi()
        {
            if (this.xinxixianshi.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshi);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.xinxixianshi.Text = string.Format(@"{0}", xianshineirong);
            }
        }

        private void xianshisudu()
        {
            if (this.gauge_speed.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshisudu);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.gauge_speed.Visible = true;
                this.digitalGauge1.DigitCount = string.Format(@"{0}", fsudu).Length < 3 ? 3 : string.Format(@"{0}", fsudu).Length;
                this.digitalGauge1.Text = string.Format(@"{0}", fsudu);
            }
        }

        private void xianshijzl()
        {
            if (this.gauge_jiazaili.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshijzl);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.gauge_jiazaili.Visible = true;
                this.digitalGauge2.DigitCount = string.Format(@"{0}", fjiazaili).Length < 3 ? 3 : string.Format(@"{0}", fjiazaili).Length;
                this.digitalGauge2.Text = string.Format(@"{0}", fjiazaili);
            }
        }

        public void init()
        {
            myPaneex = WriteGraphini(zg21);
        }
        protected void thisclose()
        {

            this.Invoke(new Action(this.Close));  //使用委托关闭该窗体
        }
        int iqsmz = 0;
        int iddzz = 0;
        int igdzz = 0;
        int igt = 0;

        List<int> listspeed = new List<int>();
        List<int> listjzl = new List<int>();
        List<int> listgl = new List<int>();
        public void StratTest()
        {
            //ShowControls(true);
            if (!flowbass.bJCGL)
            {
                xianshineirong = string.Format(@"动力性免检");
                xianshi();
                flowbass.LedWrite(flowbass.sPlate, xianshineirong, 1);

                return;
            }
            Thread.Sleep(1000);
            m_mainform.DAC.PutVol(0);
            Thread.Sleep(1000);
            flowbass.UpdateLoginRecord(DateTime.Now.ToString(), "动力性检测开始", flowbass.sDetectID);
            listspeed = new List<int>();
            listjzl = new List<int>();
            listgl = new List<int>();
            myPaneex.CurveList.Clear();
            myPaneex.GraphObjList.Clear();
            zg21.AxisChange();
            iniclass objini = new iniclass();
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\安综检仪表配置.ini";
            string path1 = string.Format(@"{0}", path);
            objini.Ini(path1);
            string sqsmz = objini.ReadValue("动力性", "起始码值").Trim();
            string sddzz = objini.ReadValue("动力性", "低点码值自增").Trim();
            string sgdzz = objini.ReadValue("动力性", "高点码值自增").Trim();
            string sgt = objini.ReadValue("动力性", "滚筒").Trim();
            xssj = 0;
            int.TryParse(objini.ReadValue("动力性", "信息显示时间").Trim(), out xssj);
            if (!int.TryParse(sqsmz, out iqsmz))
            {
                iqsmz = 800;
            }
            if (!int.TryParse(sddzz, out iddzz))
            {
                iqsmz = 40;
            }
            if (!int.TryParse(sgdzz, out igdzz))
            {
                iqsmz = 20;
            }
            if (sgt.Contains("二轴四滚筒"))
            {
                igt = 1;
            }
            else
            {
                igt = 2;
            }
            int idj = 0;
            float fedcs = 0;
            float fwdcs = 0;
            float fdbgl = 0;
            fjiazaili = 0;
            xianshineirong = string.Format(@"驶上测功台");
            xianshi();
            flowbass.LedWrite(flowbass.sPlate, "驶上测功台", 1);


            fNm = flowbass.fhdzs;//发动机额定转矩转速
            fMm = flowbass.fhdnj;//发动机额定转矩
            fPower = flowbass.fgonglv;//额定功率
            isZwLunTai = true;//子午线轮胎
            iGr = (int)flowbass.iqdzbzl;//驱动轴空载质量
            isEdgl = true;//铭牌是否以额定功率表征
            isTurbo = true;//是否涡轮增压

            fWD = 22f;//温度
            fSD = 60f;//湿度
            fDQY = 100.9f;//大气压

            // 红外定位
            //m_mainform.WaitEvent(iDLXHWQ, bhongwaiqf[iDLXHWQ], ihongwaidwsj[iDLXHWQ]);

            m_mainform.DAC.Lift(false);
            m_mainform.DAC.SetDOBit((short)iFENGSHAN, 1);
            flowbass.sendpiczj("9999", flowbass.idlxtongdao.ToString(), "dlx", "dlx");

            /////举升下降
            //if (iDLXKZJSS > 0 && iDLXKZJSJ > 0)//举升控制为1路控制时默认是设置在第一个。
            //{
            //    m_mainform.DAC.LiftTwo(false, (short)(iDLXKZJSS - 1), (short)(iDLXKZJSJ - 1));
            //}
            //else
            //{
            //    if (iDLXKZJSS > 0)
            //    {
            //        m_mainform.DAC.LiftOne(false, (short)(iDLXKZJSS - 1));
            //    }
            //    else if (iDLXKZJSJ > 0)
            //    {
            //        m_mainform.DAC.LiftOne(false, (short)(iDLXKZJSJ - 1));
            //    }
            //}
            // 提示
            xianshineirong = string.Format(@"驶上测功台。。。");
            xianshi();
            //测量数据
            DateTime dstart = DateTime.Now;
            if (flowbass.sfuel.Contains("柴油"))
            {
                flowbass.LedWrite(flowbass.sPlate, "直接档踩尽油门", 1);
                Thread.Sleep(1000);
                yrs(ref  fedcs, ref  fwdcs, ref  fdbgl, ref idj);
            }
            else
            {
                xianshineirong = string.Format(@"3档加速到指定转速");
                xianshi();
                flowbass.LedWrite("请挂3档", "加速到指定转速", 1);
                Thread.Sleep(1000);
                drs(ref  fedcs, ref  fwdcs, ref  fdbgl, ref idj);

            }

            //Thread.Sleep(1000);
            m_mainform.DAC.PutVol(0);
            Thread.Sleep(1000);
            #region 举升
            /////举升上升
            //if (iDLXKZJSS > 0 && iDLXKZJSJ > 0)//举升控制为1路控制时默认是设置在第一个。
            //{
            //    m_mainform.DAC.LiftTwo(true, (short)(iDLXKZJSS - 1), (short)(iDLXKZJSJ - 1));
            //}
            //else
            //{
            //    if (iDLXKZJSS > 0)
            //    {
            //        m_mainform.DAC.LiftOne(true, (short)(iDLXKZJSS - 1));
            //    }
            //    else if (iDLXKZJSJ > 0)
            //    {
            //        m_mainform.DAC.LiftOne(true, (short)(iDLXKZJSJ - 1));
            //    }
            //}
            m_mainform.DAC.Lift(true);
            m_mainform.DAC.SetDOBit((short)iFENGSHAN, 0);
            #endregion
            //
            //检测结束
            //test测试




            //test测试
            //保存数据
            flowbass.AddData(flowbass.sDetectID, "DET_DBGL", "Detects_SJC", fdbgl);
            flowbass.AddData(flowbass.sDetectID, "DET_EDCS", "Detects_SJC", fedcs);
            flowbass.AddData(flowbass.sDetectID, "DET_DLXJZL", "Detects_SJC", fjiazaili.ToString("0"));
            flowbass.AddData(flowbass.sDetectID, "DET_WDCS", "Detects_SJC", fwdcs);

            bool bGood = true;
            if (fwdcs < fedcs)
            {
                bGood = false;
            }

            flowbass.AddData(flowbass.sDetectID, "DET_DLXJL", "Detects_SJC", bGood ? 1 : 2);

            string jielun = "";
            string sdj = "";
            if (idj == 1)
            {
                sdj = "一级";
            }
            else
            {
                sdj = "二级";
            }
            jielun = string.Format(@"{0},{1},≥{2},{3}", "动力性(km/h)", fwdcs, fedcs, bGood ? sdj : "不合格");
            flowbass.AddData(flowbass.sDetectID, "PD_DLX", "PD_ZJ", jielun);

            flowbass.editData(flowbass.sDetectID, "DET_DLXQX", "Detects_SJC", listspeed, listgl); //  //曲线

            flowbass.editData(flowbass.sDetectID, "DET_DLXJZLQX", "Detects_SJC", listjzl, listjzl); //  //曲线

            flowbass.UpdateLoginRecord(DateTime.Now.ToString(), "动力性检测完成", flowbass.sDetectID);
            if (!bGood)
            {
                flowbass.getbuhge(flowbass.sDetectID, "DLX");
            }
            else
            {

                flowbass.sethege(flowbass.sDetectID, "DLX");
            }
        }





        public void GetSzqfy()
        {
            fSzqfy = jsjzxs(fWD, fSD);
        }
        private float fNm;//发动机额定转矩转速
        private float fMm;//发动机额定转矩
        private float fPower;//额定功率
        private bool isZwLunTai;//子午线轮胎
        private int iGr;//驱动轴空载质量
        private bool isEdgl;//铭牌是否以额定功率表征
        private bool isTurbo;//是否涡轮增压

        private float fWD = 0;//温度
        private float fSD = 0;//湿度
        private float fDQY = 0;//大气压
        private double fSzqfy = 0;//水蒸气分压

        private float fForce;
        private float fSpeed;
        //private int iPutOutFirst;
        //private int iPutOutStep;
        private float fSpeedMax;
        private float fSpeedMaxPre;
        private int fSpeedMaxCount;
        private float fSpeedStay;

        private float fPowerTimes;//功率比值系数
        private float fFE1;//一级加载力
        private float fFE2;//合格加载力
        private float fFe1;//一级车发动机达标功率换算在驱动轮表面的当量驱动力
        private float fFe2;//合格车发动机达标功率换算在驱动轮表面的当量驱动力
        private float fAd;//压燃式发动机功率校正系数
        private float fFc;//轮胎滚动阻力
        private float fFtc;//测功机台架内阻
        private float fFf;//发动机附件阻力
        private float fFt1;//一级传动系允许阻力
        private float fFt2;//合格传动系允许阻力
        //压燃式
        public void yrs(ref float fedcs, ref float fwdcs, ref float fdbgl,ref int idj)//压燃式功率仪表
        {
            idj = 1;
            #region 获取最高稳定车速
            DateTime dstart = DateTime.Now;
            fSpeedMax = 0;
            fSpeedMaxPre = 0;
            fSpeedMaxCount = 0;

            while (true)
            {
                fSpeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iDLXSDtd]);
                xianshisudu();
                if (fSpeed > 95)
                {
                    xianshineirong = string.Format(@"请降低一个档位 当前速度{0}", fSpeed);
                    xianshi();
                    flowbass.LedWrite("请降低一个档位", string.Format("当前速度{0}", fSpeed), 1);
                }
                else
                {
                    xianshineirong = string.Format(@"直接档踩尽油门 当前速度{0}", fSpeed);
                    xianshi();
                    flowbass.LedWrite("直接档踩尽油门", string.Format("当前速度{0}", fSpeed), 1);
                }
                if (fSpeed > 20)
                {
                    listspeed.Add((int)(fSpeed * 10));
                    listgl.Add(0);
                    listjzl.Add(0);
                    WriteGraph(myPaneex, zg21, listspeed, listjzl, listgl, listspeed.Count);
                }
                if (fSpeed > 50 && fSpeed < 95)
                {
                    if (Math.Abs(fSpeedMaxPre - fSpeed) < 0.3)
                    {
                        fSpeedMaxCount++;
                    }
                    else
                    {
                        fSpeedMaxCount = 0;
                    }
                    fSpeedMaxPre = fSpeed;
                    //if (fSpeedMaxPre >= fSpeed)
                    //{
                    //    if (m_mainform.DateDiff(DateTime.Now, dstart) > 1000)
                    //    {
                    //        fSpeedMaxCount++;
                    //    }
                    //    dstart = DateTime.Now;
                    //}
                    //else
                    //{
                    //    fSpeedMaxCount = 0;
                    //}
                }
                //fSpeedMaxPre = fSpeed;

                if (fSpeedMaxCount > 2)
                {
                    fSpeedMaxPre = fSpeed;
                    fSpeedMax = fSpeed;
                    break;
                }
                Thread.Sleep(800);
            }

            //fSpeedMaxCount = 0;
            //while (true)
            //{

            //    fSpeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iDLXSDtd]);
            //    xianshisudu();

            //    if (fSpeed > 95)
            //    {
            //        xianshineirong = string.Format(@"请降低一个档位 当前速度{0}", fSpeed);
            //        xianshi();
            //    }
            //    else
            //    {
            //        xianshineirong = string.Format(@"直接档踩尽油门 当前速度{0}", fSpeed);
            //        xianshi();
            //    }

            //    if (Math.Abs(fSpeedMaxPre - fSpeed) <= 1)
            //    {
            //        if (m_mainform.DateDiff(DateTime.Now, dstart) > 1000)
            //        {
            //            fSpeedMax += fSpeed;
            //            fSpeedMaxCount++;
            //        }
            //        dstart = DateTime.Now;
            //    }
            //    else
            //    {
            //        fSpeedMax = 0;
            //        fSpeedMaxCount = 0;
            //    }
            //    fSpeedMaxPre = fSpeed;
            //    if (fSpeedMaxCount > 2)
            //    {
            //        fSpeedMax = fSpeedMax / 3;
            //        break;
            //    }
            //    Thread.Sleep(300);
            //}
            #endregion

            #region 加载力计算
            fedcs = 0.86f * fSpeedMax;
            fedcs = (float)Math.Round(fedcs, 1);
            float fa = 0;
            if (isTurbo)
            {
                fa = (float)(Math.Pow((float)(99f / (fDQY - fSzqfy)), 0.7f)) * (float)(Math.Pow(((float)((273.13f + fWD) / 298f)), 1.2f));
                fAd = (float)Math.Pow(fa, 0.3);
            }
            else
            {
                fa = (float)(99f / (fDQY - fSzqfy)) * (float)(Math.Pow((float)(((273.15f + fWD) / 298f)), 0.7f));
                fAd = (float)Math.Pow(fa, 0.3);
            }
            fFe1 = 3600 * 0.82f * fPower / (fAd * fedcs);
            fFe2 = 3600 * 0.75f * fPower / (fAd * fedcs);
            float fc = 0;
            float f = 0;
            if (isZwLunTai)
            {
                f = 0.006f;
            }
            else
            {
                f = 0.01f;
            }
            if (fedcs < 70)
            {
                fc = 1.5f * f;
            }
            else
            {
                fc = 2.0f * f;
            }
            fFc = fc * iGr * 9.81f;
            //if (isEdgl)
            {
                fFf = 3600 * 0.1f * fPower / fedcs;

            }
            //else
            //{
            //    fFf = 0;
            //}
            fFt1 = 0.18f * (fFe1 - fFf);
            fFt2 = 0.18f * (fFe2 - fFf);
            if (igt == 1)
            {
                fFtc = 130;
            }
            else
            {
                fFtc = 160;
            }
            fFE1 = fFe1 - fFc - fFtc - fFf - fFt1;
            fFE2 = fFe2 - fFc - fFtc - fFf - fFt2; 
            #endregion


            flowbass.AddData(flowbass.sDetectID, "DET_YRCB1", "Detects_SJC", 0.82f);
            flowbass.AddData(flowbass.sDetectID, "DET_YRPe1", "Detects_SJC", fPower); 
            flowbass.AddData(flowbass.sDetectID, "DET_YRVe1", "Detects_SJC", fedcs);
            flowbass.AddData(flowbass.sDetectID, "DET_YRFe1", "Detects_SJC", fFe1);
            flowbass.AddData(flowbass.sDetectID, "DET_YRFEex1", "Detects_SJC", fFE1);
            flowbass.AddData(flowbass.sDetectID, "DET_YRFtc1", "Detects_SJC", fFtc);
            flowbass.AddData(flowbass.sDetectID, "DET_YRFc1", "Detects_SJC", fFc);
            flowbass.AddData(flowbass.sDetectID, "DET_YRFf1", "Detects_SJC", fFf);
            flowbass.AddData(flowbass.sDetectID, "DET_YRFt1", "Detects_SJC", fFt1);
            flowbass.AddData(flowbass.sDetectID, "DET_YRad1", "Detects_SJC", fAd);
            flowbass.AddData(flowbass.sDetectID, "DET_YRCB2", "Detects_SJC", 0.75f);
            flowbass.AddData(flowbass.sDetectID, "DET_YRPe2", "Detects_SJC", fPower);
            flowbass.AddData(flowbass.sDetectID, "DET_YRVe2", "Detects_SJC", fedcs);
            flowbass.AddData(flowbass.sDetectID, "DET_YRFe2", "Detects_SJC", fFe2);
            flowbass.AddData(flowbass.sDetectID, "DET_YRFEex2", "Detects_SJC", fFE2);
            flowbass.AddData(flowbass.sDetectID, "DET_YRFtc2", "Detects_SJC", fFtc);
            flowbass.AddData(flowbass.sDetectID, "DET_YRFc2", "Detects_SJC", fFc);
            flowbass.AddData(flowbass.sDetectID, "DET_YRFf2", "Detects_SJC", fFf);
            flowbass.AddData(flowbass.sDetectID, "DET_YRFt2", "Detects_SJC", fFt2);
            flowbass.AddData(flowbass.sDetectID, "DET_YRad2", "Detects_SJC", fAd);

            flowbass.AddData(flowbass.sDetectID, "DET_DLXWD", "Detects_SJC", fWD);
            flowbass.AddData(flowbass.sDetectID, "DET_DLXSD", "Detects_SJC", fSD);
            flowbass.AddData(flowbass.sDetectID, "DET_DLXDQY", "Detects_SJC", fDQY);

            flowbass.LedWrite("准备加力", string.Format("请稳住油门"), 1);
            Thread.Sleep(1000);
            PutPower(fedcs, fFE1);
            fdbgl = 0.82f * fPower;

            flowbass.AddData(flowbass.sDetectID, "DET_YRVw1", "Detects_SJC", fSpeedStay);
            if (fSpeedStay<fedcs)
            {
                fdbgl = 0.75f * fPower;
                idj = 2;
                flowbass.LedWrite(string.Format("二级动力性检测"), string.Format("保持油门"), 1);
                xianshineirong = string.Format(@"开始二级动力性检测，保持油门");
                xianshi();
                PutPower(fedcs, fFE2);

                xianshineirong = string.Format(@"动力性检测完成 松开油门");
                xianshi();
                flowbass.LedWrite(string.Format("动力性检测完成"), string.Format("松开油门"), 1);
                while (true)
                {
                    fSpeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iDLXSDtd]);
                    xianshisudu();
                    if (fSpeed < 1)
                    {
                        break;
                    }
                    Thread.Sleep(10);
                }
                if (fSpeedStay < fedcs)
                {
                    xianshineirong = string.Format(@"动力性检测完成，动力性不合格");
                    xianshi();
                    flowbass.LedWrite(string.Format("动力性检测完成"), string.Format("动力性不合格"), 1);
                }
                else
                {

                    xianshineirong = string.Format(@"动力性检测完成，动力性为二级");
                    xianshi();
                    flowbass.LedWrite(string.Format("动力性检测完成"), string.Format("动力性为二级"), 1);
                }
                flowbass.AddData(flowbass.sDetectID, "DET_YRVw2", "Detects_SJC", fSpeedStay);
            }
            else
            {
                xianshineirong = string.Format(@"动力性检测完成 松开油门");
                xianshi();
                flowbass.LedWrite(string.Format("动力性检测完成"), string.Format("松开油门"), 1);
                while (true)
                {
                    fSpeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iDLXSDtd]);
                    xianshisudu();
                    if (fSpeed < 1)
                    {
                        break;
                    }
                    Thread.Sleep(10);
                }
                xianshineirong = string.Format(@"动力性检测完成，动力性为一级");
                xianshi();
                flowbass.LedWrite(string.Format("动力性检测完成"), string.Format("动力性为一级"), 1);
            }

            fwdcs = (float)Math.Round( fSpeedStay,1);




        }

        public void drs(ref float fedcs, ref float fwdcs, ref float fdbgl,ref int idj)//点燃式功率仪表
        {
            #region 获取稳定车速
            DateTime dstart = DateTime.Now;
            fSpeedMax = 0;
            fSpeedMaxCount = 0;
            idj = 1;
            if (fNm > 4000)
            {
                fNm = 4000;
            }
            while (true)
            {
                fSpeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iDLXSDtd]);
                xianshisudu();
                if (fSpeed > 80)
                {
                    xianshineirong = string.Format(@"请降低一个档位 当前速度{0}", fSpeed);
                    xianshi();
                    flowbass.LedWrite(string.Format("请降低一个档位"), string.Format("当前速度{0}", fSpeed), 1);
                }
                else
                {
                    xianshineirong = string.Format(@"转速{0} 当前转速{1}", fNm, m_mainform.objzsy.m_iGasturn);
                    xianshi();
                    flowbass.LedWrite(string.Format("转速要求{0}",fNm), string.Format("转速{0}速度{1}",m_mainform.objzsy.m_iGasturn,fSpeed), 1);
                }
                if (fSpeed > 20)
                {
                    listspeed.Add((int)( fSpeed* 10));
                    listgl.Add(0);
                    listjzl.Add(0);
                    WriteGraph(myPaneex, zg21, listspeed, listjzl, listgl, listspeed.Count);
                }
                //listspeed.Add((int)(fsudu * 10));
                //listgl.Add(0);
                //listjzl.Add(0);
                //WriteGraph(myPaneex, zg2, listspeed, listjzl, listgl, listspeed.Count);
                if (fSpeed > 40)
                {
                    if (m_mainform.objzsy.m_iGasturn > fNm - 30 && m_mainform.objzsy.m_iGasturn < fNm + 30)
                    {
                        //if (m_mainform.DateDiff(DateTime.Now, dstart) > 1000)
                        {
                            fSpeedMax = fSpeed;
                            fSpeedMaxCount++;
                        }
                        dstart = DateTime.Now;
                        break;
                    }
                }
                //if (fSpeedMaxCount > 2)
                //{
                //    //fSpeedMax = fSpeedMax / 3;
                //    break;
                //}
                Thread.Sleep(800);
            }

            #endregion

            #region 加载力计算

            fSpeedMax = (float)Math.Round(fSpeedMax, 1);
            fedcs = fSpeedMax;
            float fa = 0;

            fa = (float)(Math.Pow((float)(99f / (fDQY - fSzqfy)), 1.2f)) * (float)(Math.Pow(((float)((273.13f + fWD) / 298f)), 0.6f));
            fAd = (float)Math.Pow(fa, 0.3);

            fFe1 = 0.377f * 0.82f * fMm * fNm / (fAd * fedcs);
            fFe2 = 0.377f * 0.75f * fMm * fNm / (fAd * fedcs);
            float fc = 0;
            float f = 0;
            if (isZwLunTai)
            {
                f = 0.006f;
            }
            else
            {
                f = 0.01f;
            }
            if (fedcs < 70)
            {
                fc = 1.5f * f;
            }
            else
            {
                fc = 2.0f * f;
            }
            fFc = fc * iGr * 9.81f;


            fFf = 0.377f * 0.06f * fMm * fNm / fedcs;


            fFt1 = 0.18f * (fFe1 - fFf);
            fFt2 = 0.18f * (fFe2 - fFf);

            if (igt == 1)
            {
                fFtc = 110;
            }
            else
            {
                fFtc = 140;
            }
            fFE1 = fFe1 - fFc - fFtc - fFf - fFt1;
            fFE2 = fFe2 - fFc - fFtc - fFf - fFt2;

            #endregion

            flowbass.AddData(flowbass.sDetectID, "DET_DRCB1", "Detects_SJC", 0.82f);
            flowbass.AddData(flowbass.sDetectID, "DET_DRMm1", "Detects_SJC", fMm);
            flowbass.AddData(flowbass.sDetectID, "DET_DRnm1", "Detects_SJC", fNm);
            flowbass.AddData(flowbass.sDetectID, "DET_DRFm1", "Detects_SJC", fFe1);
            flowbass.AddData(flowbass.sDetectID, "DET_DRFMex1", "Detects_SJC", fFE1);
            flowbass.AddData(flowbass.sDetectID, "DET_DRFtc1", "Detects_SJC", fFtc);
            flowbass.AddData(flowbass.sDetectID, "DET_DRFc1", "Detects_SJC", fFc);
            flowbass.AddData(flowbass.sDetectID, "DET_DRFf1", "Detects_SJC", fFf);
            flowbass.AddData(flowbass.sDetectID, "DET_DRFt1", "Detects_SJC", fFt1);
            flowbass.AddData(flowbass.sDetectID, "DET_DRaa1", "Detects_SJC", fAd);

            flowbass.AddData(flowbass.sDetectID, "DET_DRCB2", "Detects_SJC", 0.75f);
            flowbass.AddData(flowbass.sDetectID, "DET_DRMm2", "Detects_SJC", fMm);
            flowbass.AddData(flowbass.sDetectID, "DET_DRnm2", "Detects_SJC", fNm);
            flowbass.AddData(flowbass.sDetectID, "DET_DRFm2", "Detects_SJC", fFe2);
            flowbass.AddData(flowbass.sDetectID, "DET_DRFMex2", "Detects_SJC", fFE2);
            flowbass.AddData(flowbass.sDetectID, "DET_DRFtc2", "Detects_SJC", fFtc);
            flowbass.AddData(flowbass.sDetectID, "DET_DRFc2", "Detects_SJC", fFc);
            flowbass.AddData(flowbass.sDetectID, "DET_DRFf2", "Detects_SJC", fFf);
            flowbass.AddData(flowbass.sDetectID, "DET_DRFt2", "Detects_SJC", fFt2);
            flowbass.AddData(flowbass.sDetectID, "DET_DRaa2", "Detects_SJC", fAd);

            flowbass.AddData(flowbass.sDetectID, "DET_DLXWD", "Detects_SJC", fWD);
            flowbass.AddData(flowbass.sDetectID, "DET_DLXSD", "Detects_SJC", fSD);
            flowbass.AddData(flowbass.sDetectID, "DET_DLXDQY", "Detects_SJC", fDQY);

            flowbass.AddData(flowbass.sDetectID, "DET_DRVm1", "Detects_SJC", fedcs);
            //flowbass.AddData(flowbass.sDetectID, "DET_DRVw1", "Detects_SJC",fDRVw1);

            flowbass.AddData(flowbass.sDetectID, "DET_DRVm2", "Detects_SJC", fedcs);
            //flowbass.AddData(flowbass.sDetectID, "DT_DRVw2", "Detects_SJC",fDRVw2);

            PutPower(fedcs, fFE1);
            flowbass.AddData(flowbass.sDetectID, "DET_DRVw1", "Detects_SJC", fSpeedStay);//
            fdbgl = 0.82f * fMm * fNm / 9549f;
            if (fSpeedStay < fedcs)
            {
                fdbgl = 0.75f * fMm * fNm / 9549f;
                idj = 2;
                flowbass.LedWrite(string.Format("二级动力性检测"), string.Format("保持油门"), 1);
               
                xianshineirong = string.Format(@"开始二级动力性检测，保持油门");
                xianshi();
                Thread.Sleep(xssj);
                PutPower(fedcs, fFE2);

                xianshineirong = string.Format(@"动力性检测完成 松开油门");
                xianshi();
                Thread.Sleep(100);
                flowbass.LedWrite(string.Format("动力性检测完成"), string.Format("松开油门"), 1);
                while (true)
                {
                    fSpeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iDLXSDtd]);
                    xianshisudu();
                    if (fSpeed < 1)
                    {
                        break;
                    }
                    Thread.Sleep(10);
                }
                if (fSpeedStay < fedcs)
                {
                    xianshineirong = string.Format(@"动力性检测完成，动力性不合格");
                    xianshi();
                    Thread.Sleep(100);
                    flowbass.LedWrite(string.Format("动力性检测完成"), string.Format("动力性不合格"), 1);
                }
                else
                {

                    xianshineirong = string.Format(@"动力性检测完成，动力性为二级");
                    xianshi();
                    flowbass.LedWrite(string.Format("动力性检测完成"), string.Format("动力性为二级"), 1);
                }
                flowbass.AddData(flowbass.sDetectID, "DET_DRVw2", "Detects_SJC", fSpeedStay);
            }
            else
            {
                xianshineirong = string.Format(@"动力性检测完成 松开油门");
                xianshi();
                Thread.Sleep(100);
                flowbass.LedWrite(string.Format("动力性检测完成"), string.Format("松开油门"), 1);
                while (true)
                {
                    fSpeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iDLXSDtd]);
                    xianshisudu();
                    if (fSpeed < 1)
                    {
                        break;
                    }
                    Thread.Sleep(10);
                }
                xianshineirong = string.Format(@"动力性检测完成，动力性为一级");
                xianshi();
                flowbass.LedWrite(string.Format("动力性检测完成"), string.Format("动力性为一级"), 1);
            }
            fSpeedStay = (float)Math.Round(fSpeedStay, 1);
            fwdcs = fSpeedStay;
        }

        private void PutPower(float fedcs, float fFE)
        {
            float fpowerdq = 0;
            DateTime dstart = DateTime.Now;
            m_mainform.DAC.PutVol(iqsmz);
            //iPutOutStep = 0;
            fSpeedStay = 0;
            #region  加力过程
            int ijzmz = 0;
            while (true)
            {
                fSpeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iDLXSDtd]);
                xianshisudu();
                fForce = (m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIData[iDLXtdz]) - m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataZero[iDLXtdz]) + m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIData[iDLXtdy]) - m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataZero[iDLXtdy]) + m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIData[iDLXtdh]) - m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataZero[iDLXtdh]))*10f;
                fForce = flics * fForce;
                fjiazaili = (float)Math.Round(fForce, 0);
                xianshijzl();

                fpowerdq = fSpeed * fjiazaili / 360f;
                listspeed.Add((int)(fSpeed * 10));
                listgl.Add((int)(fpowerdq * 10));
                listjzl.Add((int)fjiazaili);
                WriteGraph(myPaneex, zg21, listspeed, listjzl, listgl, listspeed.Count);

                xianshineirong = string.Format("额定车速{0}", fedcs) + string.Format("当前{0}", fSpeed);
                xianshi();
                flowbass.LedWrite(string.Format("额定车速{0}", fedcs), string.Format("当前{0}", fSpeed), 1);
                if (Math.Abs(fFE - fForce) <= 20)
                {
                    ijzmz = iqsmz;
                    break;
                }
                if (fForce + 20 < fFE)
                {
                    if (iqsmz > 1500)
                    {
                        iqsmz += igdzz;
                    }
                    else
                    {
                        iqsmz += iddzz;
                    }
                }
                if (fForce - 20 > fFE)
                {
                    if (iqsmz > 1500)
                    {
                        iqsmz -= igdzz / 3;
                    }
                    else
                    {
                        iqsmz -= iddzz / 3;
                    }
                }

                m_mainform.DAC.PutVol(iqsmz);
                Thread.Sleep(500);
            }
            #endregion

            #region  取数过程
            int count = 1;
            float fspeedwd = 0;
            //fSpeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iDLXSDtd]);
            //fspeedwd = fSpeed;
            while (true)
            {
                fSpeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iDLXSDtd]);
                xianshisudu();
                fForce = (m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIData[iDLXtdz]) - m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataZero[iDLXtdz]) + m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIData[iDLXtdy]) - m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataZero[iDLXtdy]) + m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIData[iDLXtdh]) - m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataZero[iDLXtdh])) * 10f;
                fForce = flics * fForce;
                //fForce = m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIData[iDLXtdz]) * 10f + m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIData[iDLXtdy]) * 10f + m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIData[iDLXtdh]) * 10f;
                fForce = fFE - 10f + (float)m_mainform.getRandom(0, 15);
                fjiazaili = fForce;
                xianshijzl();
                flowbass.LedWrite(string.Format("额定车速{0}", fedcs), string.Format("当前{0}剩{1}秒", fSpeed, 6 - count), 1);

                xianshineirong = string.Format("额定车速{0}", fedcs) + string.Format("当前{0}剩{1}秒", fSpeed, 6 - count);
                xianshi();
                fpowerdq = fSpeed * fjiazaili / 360f;
                listspeed.Add((int)(fSpeed * 10));
                listjzl.Add((int)fjiazaili);
                listgl.Add((int)(fpowerdq * 10));
                WriteGraph(myPaneex, zg21, listspeed, listjzl, listgl, listspeed.Count);
                if (Math.Abs(fspeedwd - fSpeed) <= 0.5)
                {
                    count++;
                }
                else
                {
                    fspeedwd = fSpeed;
                    count = 0;
                }
                if (count > 5)
                {
                    fSpeedStay = fSpeed;
                    break;
                }
                m_mainform.DAC.PutVol(ijzmz);
                Thread.Sleep(800);
            }
            #endregion
        }

        public void Thread1()
        {
            Thread.Sleep(3000);
            try
            {
                StratTest();
                if (flowbass.LastProcessnewzj(int.Parse(flowbass.sDetectID)) && (flowbass.bzj || flowbass.bsj))
                {
                    xianshineirong = "检测线完成";
                    flowbass.UpdateLoginRecord(DateTime.Now.ToString(), "检测线完成", flowbass.sDetectID);
                    flowbass.LedWrite(flowbass.sPlate, xianshineirong, 1);
                    flowbass.sethegezj(flowbass.sDetectID);
                    Thread.Sleep(2000);
                }
                flowbass.LedWrite(flowbass.sgongwei, "", 1);
                xianshineirong = flowbass.sgongwei;
                xianshi();
                end(); openshangxian();
                flowbass.getProcessfinish(flowbass.sDetectID, "DLX");
                //t1.Abort();
                //t1.DisableComObjectEagerCleanup();

            }
            catch (Exception ex)
            {
                openshangxian();
                //错误日记 记录到当天日期下目录中
                m_mainform.baocunriji("动力性", ex.Message.ToString());
            }
        }
        public void Thread2()
        {
           

        }
         public int  panel_Width;
         public int panel_Height; 
        private void TESTDLX_Resize(object sender, EventArgs e)
        {
            //float newx = (panel_Width) / x;
            //float newy = (panel_Height) / y;
            //setControls(newx, newy, this);
        }

        private void TESTDLX_Load(object sender, EventArgs e)
        {
           
        }
        float flics = 1;
        public void bianliangchushihuaJC()
        {
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\检测要求设置.ini";
            string path1 = string.Format(@"{0}", path);
            Ini(path1);
            float.TryParse(ReadValue("设置", "力信号参数"), out flics);


        }
        double jsjzxs(float fwd, float fsd)
        {
            double[] fwdsz = { -10, -5, 0, 5, 10, 15, 20, 25, 27, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50 };
            double[] fsdsz = { 0.2, 0.4, 0.6, 0.8, 1 };
            double[,] fszqfysz ={{0.1,0.1,0.2,0.2,0.3},
		{0.1,0.2,0.2,0.3,0.4},
		{0.1,0.2,0.4,0.5,0.6},
		{0.2,0.4,0.5,0.7,0.9},
		{0.2,0.5,0.7,1,1.2},
		{0.5,0.7,1,1.4,1.7},
		{0.5,0.9,1.4,1.9,2.3},
		{0.6,1.3,1.9,2.5,3.2},
		{0.7,1.4,2.1,2.9,3.6},
		{0.9,1.7,2.5,3.4,4.2},
		{1,1.9,2.9,3.8,4.8},
		{1.1,2.1,3.2,4.3,5.3},
		{1.2,2.6,3.6,4.8,6},
		{1.3,2.7,4,5.3,6.6},
		{1.5,3,4.4,5.9,7.4},
		{1.6,3.3,4.9,6.6,8.2},
		{1.8,3.6,5.5,7.3,9.1},
		{2,4,6.1,8.1,10.1},
		{2.2,4.5,6.7,8.9,11.2},
	    {2.5,4.9,7.4,9.9,12.3}	
	};
            double fszqfy = 1.9;
            bool bbz = false;
            for (int i = 0; i < 5; i++)
            {
                if (fsd <= fsdsz[i] * 100)
                {
                    for (int j = 0; j < 20; j++)
                    {
                        if (fwd <= fwdsz[j])
                        {
                            fszqfy = fszqfysz[j, i];
                            bbz = true;
                            break;
                        }


                    }
                    if (bbz)
                        break;
                }
            }
            return fszqfy;
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            m_mainform.DAC.Lift(true);
        }

        private void btnDown_Click(object sender, EventArgs e)
        { 
            m_mainform.DAC.Lift(false);
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            start();// ("1");
        }

        private void btnCurve_Click(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (flowbass.izhuangtai == 2)
            {
                m_mainform.objyibiaobass.select(flowbass.sshebei);
                m_mainform.bpanelshow = true;
                m_mainform.showpanel();
                flowbass.izhuangtai = 1;
                //Thread tt = new Thread(end);
                //tt.Start();
                flowbass.IsReadyForStart();
                bianliangchushihua();
                t1 = new Thread(new ThreadStart(Thread1));
                t1.Start();
                //this.ShowDialog();
            }
        }

        public void timerstart()
        {
            timer1.Enabled = true;
        }
        public void end()
        {
            
            flowbass.izhuangtai = 0;
            flowbass.sendend("E", flowbass.sDetectID);
            //closeform();
        }

        private void closeform()
        {
            if (this.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(closeform);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.Dispose();
                this.Close();
            }
        }


        private void openshangxian()
        {
            if (this.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(openshangxian);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                toolStripButton4.Enabled = true;
            }
        }
        private void 仪表设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            工位配置.DLXsetting obj = new 工位配置.DLXsetting();
            obj.ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            flowbass.izhuangtai = 1;
            //Thread tt = new Thread(end);
            //tt.Start();
            flowbass.IsReadyForStart();
            bianliangchushihua();
           // m_mainform.DAC.tiaoling(1);
            t1 = new Thread(new ThreadStart(Thread1));
            t1.Start();
        }

        private void TESTDLX_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                t1.Abort();
                t1.DisableComObjectEagerCleanup();
            }
            catch 
            { }
            flowbass.izhuangtai = 0;
            //flowbass.sendend("E", flowbass.sDetectID);
        }

        GraphPane myPaneex;
        public GraphPane WriteGraphini(ZedGraphControl zgc)
        {
            //zgc.Dispose();

            //判断每条曲线的最大值
            double ispmax = 0;
            ispmax = 100;

            double ili = 0;
            ili = 500;//ili * 1.3;


            double iyouhao = 0;
            iyouhao = 50;//iyouhao * 1.4 / 1000;

            //zgc.GraphPane.
            GraphPane myPane = zgc.GraphPane;
            zgc.IsShowPointValues = true;//数据节点
            // Set the titles and axis labels
            myPane.Title.Text = string.Format(@"动力性过程曲线");
            myPane.XAxis.Title.Text = "时间, s";
            myPane.YAxis.Title.Text = "速度, km/h";
            myPane.Y2Axis.Title.Text = "功率, kw";

            // Make up some data points based on the Sine function
            PointPairList vList = new PointPairList();
            PointPairList aList = new PointPairList();
            PointPairList dList = new PointPairList();
            PointPairList eList = new PointPairList();

            //Generate a red curve with diamond symbols, and "Velocity" in the legend
            LineItem myCurve = myPane.AddCurve("速度",
               vList, Color.Red, SymbolType.None);
            myCurve.Line.Width = 1.7F;
            // Fill the symbols with white
            myCurve.Symbol.Fill = new Fill(Color.White);

            // Generate a blue curve with circle symbols, and "Acceleration" in the legend
            myCurve = myPane.AddCurve("功率",
               aList, Color.Blue, SymbolType.None);
            myCurve.Line.Width = 1.7F;
            // Fill the symbols with white
            myCurve.Symbol.Fill = new Fill(Color.White);
            // Associate this curve with the Y2 axis
            myCurve.IsY2Axis = true;

            // Generate a green curve with square symbols, and "Distance" in the legend
            myCurve = myPane.AddCurve("加载力",
               dList, Color.Lime, SymbolType.None);
            myCurve.Line.Width = 1.7F;
            // Fill the symbols with white
            myCurve.Symbol.Fill = new Fill(Color.White);
            // Associate this curve with the second Y axis
            myCurve.YAxisIndex = 1;



            // Show the x axis grid
            myPane.XAxis.MajorGrid.IsVisible = true;

            // Make the Y axis scale red
            myPane.YAxis.Scale.FontSpec.FontColor = Color.Red;
            myPane.YAxis.Title.FontSpec.FontColor = Color.Red;
            // turn off the opposite tics so the Y tics don't show up on the Y2 axis
            myPane.YAxis.MajorTic.IsOpposite = false;
            myPane.YAxis.MinorTic.IsOpposite = false;
            // Don't display the Y zero line
            myPane.YAxis.MajorGrid.IsZeroLine = false;
            // Align the Y axis labels so they are flush to the axis
            myPane.YAxis.Scale.Align = AlignP.Inside;
            myPane.YAxis.Scale.Max = ispmax;//最大值

            // Enable the Y2 axis display
            myPane.Y2Axis.IsVisible = true;
            // Make the Y2 axis scale blue
            myPane.Y2Axis.Scale.FontSpec.FontColor = Color.Blue;
            myPane.Y2Axis.Title.FontSpec.FontColor = Color.Blue;
            // turn off the opposite tics so the Y2 tics don't show up on the Y axis
            myPane.Y2Axis.MajorTic.IsOpposite = false;
            myPane.Y2Axis.MinorTic.IsOpposite = false;
            // Display the Y2 axis grid lines
            myPane.Y2Axis.MajorGrid.IsVisible = true;
            // Align the Y2 axis labels so they are flush to the axis
            myPane.Y2Axis.Scale.Align = AlignP.Inside;
            myPane.Y2Axis.Scale.Min = 0;
            myPane.Y2Axis.Scale.Max = iyouhao;

            // Create a second Y Axis, green
            YAxis yAxis3 = new YAxis("加载力");
            myPane.YAxisList.Add(yAxis3);
            //myPane.Y2AxisList
            yAxis3.Scale.FontSpec.FontColor = Color.Lime;
            yAxis3.Title.FontSpec.FontColor = Color.Lime;
            yAxis3.Color = Color.Green;
            // turn off the opposite tics so the Y2 tics don't show up on the Y axis
            yAxis3.MajorTic.IsInside = false;
            yAxis3.MinorTic.IsInside = false;
            yAxis3.MajorTic.IsOpposite = false;
            yAxis3.MinorTic.IsOpposite = false;
            yAxis3.Scale.Max = ili;
            // Align the Y2 axis labels so they are flush to the axis
            //   yAxis3.Scale.Align = AlignP.Inside;



            // Fill the axis background with a gradient
            myPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45.0f);

            zgc.AxisChange();
            zgc.Invalidate();
            return myPane;
        }
        public void WriteGraph(GraphPane myPane, ZedGraphControl zgc, List<int> sspeed, List<int> sli, List<int> syouhao, int icount)
        {
            double ispmax = 0;
            for (int i = 0; i < icount; i++)
            {

                if (ispmax < (sspeed[i]))
                {
                    ispmax = (sspeed[i]);
                }

            }

            double ili = 0;
            for (int i = 0; i < icount; i++)
            {

                if (ili < (sli[i]))
                {
                    ili = (sli[i]);
                }

            }


            double iyouhao = 0;
            for (int i = 0; i < icount; i++)
            {

                if (iyouhao < (syouhao[i]))
                {
                    iyouhao = (syouhao[i]);
                }

            }
            if (myPane.YAxis.Scale.Max < ispmax)
            {
                myPane.YAxis.Scale.Max = ispmax * 1.1 / 10;
            }
            if (myPane.Y2Axis.Scale.Max < iyouhao)
            {
                myPane.Y2Axis.Scale.Max = iyouhao * 1.2 / 10;
            }
            if (myPane.YAxisList["加载力"].Scale.Max < ili)
            {
                myPane.YAxisList["加载力"].Scale.Max = ili;
            }
            // Make up some data points based on the Sine function
            PointPairList vList = new PointPairList();
            PointPairList aList = new PointPairList();
            PointPairList dList = new PointPairList();

            // Fabricate some data values

           
            for (int i = 0; i < icount; i++)
            {
                double time = (double)i;

                vList.Add(time, ((float)sspeed[i]) / 10f);


                aList.Add(time, (float)((float)syouhao[i]) / 10f);

                dList.Add(time, (sli[i]));




            }

            // Generate a red curve with diamond symbols, and "Velocity" in the legend
            LineItem myCurve = myPane.AddCurve("",
               vList, Color.Red, SymbolType.None);
            myCurve.Line.Width = 1.7F;
            // Fill the symbols with white
            //myCurve.Symbol.Fill = new Fill(Color.White);

            // Generate a blue curve with circle symbols, and "Acceleration" in the legend
            myCurve = myPane.AddCurve("",
               aList, Color.Blue, SymbolType.None);
            myCurve.Line.Width = 1.7F;
            // Fill the symbols with white
            //myCurve.Symbol.Fill = new Fill(Color.White);
            // Associate this curve with the Y2 axis
            myCurve.IsY2Axis = true;

            // Generate a green curve with square symbols, and "Distance" in the legend
            myCurve = myPane.AddCurve("",
               dList, Color.Lime, SymbolType.None);
            myCurve.Line.Width = 1.7F;
            // Fill the symbols with white
            //myCurve.Symbol.Fill = new Fill(Color.White);
            // Associate this curve with the second Y axis
            myCurve.YAxisIndex = 1;

            zgc.AxisChange();
            zgc.Invalidate();
        }

        private void 显示控件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowControls(true);
        }
        public void ShowControls(bool flag)
        {

            groupBox1.Visible = flag;
            //zg21.Visible = flag;
            label1.Visible = flag;
            label2.Visible = flag;
            btnTest.Visible = flag;
            btnUp.Visible = flag;
            btnDown.Visible = flag;
            button1.Visible = flag;
            but_tiaoling.Visible = flag;

        }
        public void ShowButtons(bool flag)
        {
            groupBox1.Visible = flag;
            //zg21.Visible = flag;
            btnTest.Visible = flag;
            btnUp.Visible = flag;
            btnDown.Visible = flag;
            button1.Visible = flag;
            but_tiaoling.Visible = flag;
        }

        private void but_tiaoling_Click(object sender, EventArgs e)
        {
            m_mainform.DAC.tiaoling(1);
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {

        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            m_mainform.DAC.Lift(true);
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            m_mainform.DAC.Lift(false);
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            start();// ("1");
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            toolStripButton4.Enabled = false;
            flowbass.izhuangtai = 1;
            //Thread tt = new Thread(end);
            //tt.Start();
            flowbass.IsReadyForStart();
            bianliangchushihua();
            // m_mainform.DAC.tiaoling(1);
            if (flowbass.dlxsxpd(flowbass.sDetectID))
            {
                if (MessageBox.Show(flowbass.sPlate +"   已经检测过动力性，是否再次检测？", "提示", MessageBoxButtons.YesNo) == DialogResult.No)
                {
                    openshangxian();
                    return;
                }
                else
                {
                    t1 = new Thread(new ThreadStart(Thread1));
                    t1.Start();
                }
            }
            else
            {
                t1 = new Thread(new ThreadStart(Thread1));
                t1.Start();
            }
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            m_mainform.DAC.tiaoling(1);
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            openshangxian();
            try
            {
                t1.Abort();
                t1.DisableComObjectEagerCleanup();
            }
            catch
            { }
            m_mainform.DAC.SetDOBit((short)iFENGSHAN, 0);
            flowbass.izhuangtai = 0;
            xianshineirong = string.Format(@"动力性下线");
            xianshi();
            flowbass.LedWrite(flowbass.sPlate, xianshineirong, 1);
            Thread.Sleep(1000);
            flowbass.LedWrite(flowbass.sgongwei, "", 1);
            m_mainform.DAC.PutVol(0);
        }

        private void TESTDLX_Shown(object sender, EventArgs e)
        {
            try
            {
                float newx = (this.Width) / x;
                float newy = (this.Height) / y;
                setControls(newx, newy, this);
                ShowButtons(false);
                this.BackgroundImage = Image.FromFile(@"./ico/动力性背景.jpg");
                this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;

            }
            catch (Exception ex)
            {

                m_mainform.baocunriji("动力性Load失败", ex.Message.ToString());//problem
                m_mainform.SaveErrorLog("动力性Load失败", ex.Message.ToString());//problem

            }
        }
    }
}
